package com.rk.kbk.controller;

/**
 * Created by bilibili on 2017/3/13.
 */


import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.UserDao;
import com.rk.kbk.dao.WithdrawMesgDao;
import com.rk.kbk.model.User;
import com.rk.kbk.model.WithdrawMesg;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @WithdrawMesgController
 * @银行卡Controller
 * @version : Ver 1.0
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/withdrawMesg")
public class WithdrawMesgController {
    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private WithdrawMesgDao withdrawMesgDao;

    @Autowired
    private UserDao userDao;
    /**
     * @银行卡详情byuserId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/detailsByUserId",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp DetailsByUserId(Long userId) {
        log.debug("银行卡详情byuserId");
        return JsonResp.ok(withdrawMesgDao.findByUserIdOrderByIdAsc(userId));
    }

    /**
     * @银行卡详情byId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/detailsById",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp DetailsById(Long id) {
        log.debug("银行卡详情byuserId");
        return JsonResp.ok(withdrawMesgDao.findOne(id));
    }

    /**
     * @修改银行卡
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/update",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp update(Long id,String bankNum,String realName,String bankName,String obligateMobile) {
        log.debug("修改银行卡");
        WithdrawMesg w = withdrawMesgDao.findOne(id);
        w.setBankName(bankName);
        w.setBankNum(bankNum);
        w.setObligateMobile(obligateMobile);
        w.setRealName(realName);
        withdrawMesgDao.save(w);
        return JsonResp.ok();
    }





    /**
     * @保存提现银行卡信息（已存在不做保存）
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/saveCardMess",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp saveCardMess(@RequestBody WithdrawMesg withdrawMesg) {
        log.debug("保存提现银行卡信息（已存在不做保存）");
        String bankNum=withdrawMesg.getBankNum();
        WithdrawMesg withdrawMesgOld=withdrawMesgDao.findByBankNum(bankNum);
        if(withdrawMesgOld!=null){
            return JsonResp.ok();
        }else {
            User user=userDao.findOne(withdrawMesg.getUserId());
            withdrawMesg.setObligateMobile(user.getPhone());
            withdrawMesgDao.save(withdrawMesg);
            return  JsonResp.ok();

        }

    }



    /**
     * @获取银行卡列表
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/getBandCardList",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp getBandCardList(Long userId) {
        log.debug("获取银行卡列表");
        List<WithdrawMesg> withdrawMesgList=withdrawMesgDao.findByUserIdOrderByIdAsc(userId);
        return JsonResp.ok(withdrawMesgList);

    }



}

